package cn.wgx.modules.sys.services;

import cn.wgx.commons.util.StringUtils;
import cn.wgx.modules.sys.entity.SysNotice;
import cn.wgx.modules.sys.entity.SysNoticeExample;
import cn.wgx.modules.sys.entity.SysUser;
import cn.wgx.modules.sys.entity.mapper.SysNoticeMapper;
import cn.wgx.modules.sys.util.UserUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SysNoticeService {

    @Resource
    SysNoticeMapper sysNoticeMapper;

    public int insertBatch(List<Integer> ids, String content, String title, Integer type) {
        int count = 0;
        for (Integer id : ids) {
            SysNotice sysNotice = new SysNotice();
            sysNotice.setDel(0);
            sysNotice.setType(type);
            sysNotice.setTitle(title);
            sysNotice.setContent(content);
            sysNotice.setSchoolId(id);
            sysNotice.setCreateUserId(UserUtil.getUser().getUser_id());
            sysNotice.setUpdateUserId(UserUtil.getUser().getUser_id());
            count += sysNoticeMapper.insertSelective(sysNotice);
        }
        return count;
    }

    public int insertSquad(List<Integer> ids, String content, String title, Integer type) {
        SysUser user = UserUtil.getUser();
        int count = 0;
        for (Integer id : ids) {
            SysNotice sysNotice = new SysNotice();
            sysNotice.setDel(0);
            sysNotice.setType(type);
            sysNotice.setTitle(title);
            sysNotice.setContent(content);
//            sysNotice.setSchoolId(user.getSchool_id());//todo
            sysNotice.setSquadId(id);
            sysNotice.setCreateUserId(user.getUser_id());
            sysNotice.setUpdateUserId(user.getUser_id());
            count += sysNoticeMapper.insertSelective(sysNotice);
        }
        return count;

    }

    public List<SysNotice> getList(SysNotice sysNotice) {
        SysUser user = UserUtil.getUser();
        SysNoticeExample example = new SysNoticeExample();
        SysNoticeExample.Criteria criteria = example.createCriteria();
        SysNoticeExample.Criteria criteria2 = example.createCriteria();
        if (StringUtils.isNotBlank(sysNotice.getTitle())) {
            criteria.andTitleLike("%" + sysNotice.getTitle() + "%");
        }
        if (StringUtils.isNotBlank(sysNotice.getContent())) {
            criteria.andContentLike("%" + sysNotice.getContent() + "%");
        }
//        if (sysNotice.getSchoolId() != null && sysNotice.getSchoolId() != 0) {
//            criteria.andSchoolIdBetween(0, user.getSchool_id());//当前登录者所在院校
//        }
//        if (sysNotice.getSquadId() != null) {
//            criteria.andSquadIdBetween(0, user.getSquad_id());//当前登录者所在班级
//        }
        example.setOrderByClause("type desc , createDate desc");
        return sysNoticeMapper.selectByExample(example);
    }

    public int updateStatus(Integer notice_id) {
        SysNoticeExample example = new SysNoticeExample();
        example.createCriteria().andNotice_idEqualTo(notice_id).andTypeEqualTo(0);
        SysNotice sysNotice = new SysNotice();
        sysNotice.setType(1);
        return sysNoticeMapper.updateByExampleSelective(sysNotice, example);
    }

    public int deleteBatch(List<Integer> ids, boolean logic) {
        int count = 0;
        if (logic) {//逻辑删除 del = 1
            SysNotice sysNotice = new SysNotice();
            for (Integer id : ids) {
                sysNotice.setNotice_id(id);
                sysNotice.setDel(1);
                count += sysNoticeMapper.updateByPrimaryKeySelective(sysNotice);
            }
        } else {//彻底删除
            SysNoticeExample example = new SysNoticeExample();
            for (Integer id : ids) {
                example.createCriteria().andNotice_idEqualTo(id).andDelEqualTo(1);
                count += sysNoticeMapper.deleteByExample(example);
            }
        }
        return count;
    }

    public SysNotice getOne(Integer id) {
        return sysNoticeMapper.selectByPrimaryKey(id);
    }
}
